package com.woniu;

import cn.hutool.core.io.FileUtil;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Hello world!
 */
public class GeneratorApp {
    private static String backupLocations;
    private static String mapperLocations;

    static {
        Properties properties = new Properties();
        try(InputStream in = GeneratorApp.class.getClassLoader().getResourceAsStream("db.properties")) {
            properties.load(in);
            // portal\src\main\java
            String targetEntityProject = properties.getProperty("targetEntityProject");
            // com.woniu.protal.pojo
            String targetMapperPackage = properties.getProperty("targetMapperPackage");
            // portal.src.main.java
            targetEntityProject = targetEntityProject.replace("\\", ".");
            // portal.src.main.java.com.woniu.protal.mapper
            mapperLocations = targetEntityProject + "." + targetMapperPackage;
            // portal/src/main/java/com/woniu/protal/mapper
            mapperLocations = mapperLocations.replace(".", File.separator);
            // portal/src/main/java/com/woniu/protal/backup
            backupLocations = mapperLocations.replace("mapper", "backup");
            backupLocations = backupLocations.replace(".", File.separator);

            System.out.println(mapperLocations);
            System.out.println("---------------------");
            System.out.println(backupLocations);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void mbg() {
        try {
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
//            File configFile = new File("generator.xml");
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(
                    GeneratorApp.class.getClassLoader().getResourceAsStream("generator.xml")
            );
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void backup() {
        File mapperFolders = new File(mapperLocations);
        File[] files = mapperFolders.listFiles();
        // 如果读取到了这个目录
        if (!(files == null || files.length == 0)) {
            // 读取backup的文件
            File backupFolders = new File(backupLocations);
            // 复制
            FileUtil.copyContent(mapperFolders, backupFolders, true);
        }else {
            System.out.println("因为主目录没有文件，不需要备份");
        }
    }


    private static void restore() {

        File backupFolders = new File(backupLocations);
        File[] files = backupFolders.listFiles();
        // 如果读取到了这个目录
        if (!(files == null || files.length == 0)) {
            // 读取mapper的文件
            File mapperFolders = new File(mapperLocations);
            // 复制
            FileUtil.copyContent(backupFolders, mapperFolders, true);
            // 备份目录删除
            FileUtil.del(backupFolders);
        }
    }

    public static void main(String[] args) {
        backup();
        mbg();
//        restore();
    }
}
